package com.lancoo.klgcourseware.presenter;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.lancoo.klgcourseware.api.KlgEnglishApi;
import com.lancoo.klgcourseware.base.BaseKlgPresenter;
import com.lancoo.klgcourseware.entity.EnglishCardUsageTrain;
import com.lancoo.klgcourseware.entity.ExpressPracticeModel;
import com.lancoo.klgcourseware.entity.KlgApiResponse;
import com.lancoo.klgcourseware.entity.MoreSlideMenuModel;
import com.lancoo.klgcourseware.entity.WordArticulation;
import com.lancoo.klgcourseware.entity.WordPracticeModel;
import com.lancoo.klgcourseware.entity.bean.ErrorStates;
import com.lancoo.klgcourseware.entity.bean.KlgCommonBean;
import com.lancoo.klgcourseware.entity.bean.KlgConstant;
import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;
import com.lancoo.klgcourseware.entity.bigData.PushResponse;
import com.lancoo.klgcourseware.entity.bigData.PushResponseBean;
import com.lancoo.klgcourseware.view.IBasicTrainView;
import com.rxjava.rxlife.RxLife;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import lancoo.com.net.retrofitrxjava.NetworkTransformer;
import lancoo.com.net.retrofitrxjava.network.Network;
import lancoo.com.net.retrofitrxjava.subscriber.NormalSubscriber;

/**
 * Created by pengmi on 2019/8/15 16:55
 * Description:
 */
public class BasicTrainPresenter extends BaseKlgPresenter {

    private IBasicTrainView iBasicTrainView;
    private String klgCode;

    public BasicTrainPresenter(Context mContext, String klgCode, IBasicTrainView iBasicTrainView, LifecycleOwner owner) {
        super(mContext,owner);
        this.klgCode=klgCode;
        this.iBasicTrainView=iBasicTrainView;
    }



    public void requestInfo(){
        iBasicTrainView.showLoadingPg();
        Network.getJsonData(KlgEnglishApi.class, KlgDbConstant.klg_baseUrl).getWordSoundTrain(klgCode).compose(NetworkTransformer.<KlgApiResponse<WordArticulation>>commonSchedulers())
                .as(RxLife.<KlgApiResponse<WordArticulation>>as(this))
                .subscribe(new NormalSubscriber<KlgApiResponse<WordArticulation>>(){
                    @Override
                    public void onNext(KlgApiResponse<WordArticulation> listKlgApiResponse) {
                        super.onNext(listKlgApiResponse);
                        requestUsageInfo(listKlgApiResponse.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        iBasicTrainView.loadingFailure(ErrorStates.NETERROR);
                    }
                });
    }


    private void requestUsageInfo(final WordArticulation wordArticulation){
        iBasicTrainView.showLoadingPg();
        Network.getJsonData(KlgEnglishApi.class, KlgDbConstant.klg_baseUrl).getEnglishPractice(klgCode).compose(NetworkTransformer.<KlgApiResponse<EnglishCardUsageTrain>>commonSchedulers())
                .as(RxLife.<KlgApiResponse<EnglishCardUsageTrain>>as(this))
                .subscribe(new NormalSubscriber<KlgApiResponse<EnglishCardUsageTrain>>(){

                    @Override
                    public void onNext(KlgApiResponse<EnglishCardUsageTrain> englishCardUsageTrainKlgApiResponse) {
                        super.onNext(englishCardUsageTrainKlgApiResponse);
                        if(englishCardUsageTrainKlgApiResponse.getState()==1&&englishCardUsageTrainKlgApiResponse.getData()!=null){
                            List<ExpressPracticeModel> expressPracticeModels = buildEnglishCardUsageTrainList(englishCardUsageTrainKlgApiResponse.getData());
                            iBasicTrainView.loadingSuccess(wordArticulation,expressPracticeModels);
                        }else{
                            iBasicTrainView.loadingFailure(ErrorStates.DATAERROR);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        iBasicTrainView.loadingFailure(ErrorStates.NETERROR);
                    }
                });
    }


    private List<ExpressPracticeModel> buildEnglishCardUsageTrainList(EnglishCardUsageTrain englishCardUsageTrain){
        List<ExpressPracticeModel> englishCardUsageTrainList=new ArrayList<>();
        switch (englishCardUsageTrain.getKlgType()){
            case "C":
            case "D":
            case "G":
                for (WordPracticeModel wordPracticeModel:englishCardUsageTrain.getWordSenList()){
                    ExpressPracticeModel oneSentenceModel=new ExpressPracticeModel();
                    List<WordPracticeModel> wordPracticeModels=new ArrayList<>();
                    wordPracticeModels.add(wordPracticeModel);
                    oneSentenceModel.setOneSentenceList(wordPracticeModels);
                    englishCardUsageTrainList.add(oneSentenceModel);
                }
                break;
            case "E":
                englishCardUsageTrainList.addAll(englishCardUsageTrain.getExpressSenList());
                break;
            case "F":
            case "H"://英语文体
            case "J"://英语构词法
            case "K"://英语词法
            case "M"://英语常用句式
                for (WordPracticeModel wordPracticeModel:englishCardUsageTrain.getSyntaxSenList()){
                    ExpressPracticeModel oneSentenceModel=new ExpressPracticeModel();
                    List<WordPracticeModel> wordPracticeModels=new ArrayList<>();
                    wordPracticeModels.add(wordPracticeModel);
                    oneSentenceModel.setOneSentenceList(wordPracticeModels);
                    englishCardUsageTrainList.add(oneSentenceModel);
                }
                break;
        }
        return englishCardUsageTrainList;
    }

    public void pushKnowledgeEvent(MoreSlideMenuModel moreSlideMenuModel, KlgCommonBean bean){
        PushResponse pushResponse=new PushResponse();
        pushResponse.setUserID(KlgConstant.userID);
        pushResponse.setKnowledgeCode(klgCode);
        pushResponse.setKnowledge(bean.getKlgName());
        pushResponse.setSysID("A00");
        pushResponse.setSubjectID(KlgConstant.subjectID);
        pushResponse.setStuScore(moreSlideMenuModel.getTrainScore());
        pushResponse.setQuesScore(100);
        pushResponse.setAnswerTime((int) (moreSlideMenuModel.getTrainTime()));
        pushResponse.setSchoolID(KlgConstant.schoolID);
        pushResponse.setMExamTypeCode(KlgConstant.mExamTypeCode);
        String abilityType="LO";
        String moduleName = moreSlideMenuModel.getModuleName();
        if(moduleName.contains("填空")){
            abilityType="RS";
        }else if(moduleName.contains("听音")||moduleName.contains("口语")){
            abilityType="LS";
        }
        pushResponse.setAbilityType(abilityType);
        Network.getJsonData(KlgEnglishApi.class, KlgDbConstant.klgPublicUrl)
                .pushKnowledgeEvent(pushResponse)
                .compose(NetworkTransformer.<PushResponseBean>commonSchedulers())
                .subscribe(new NormalSubscriber<PushResponseBean>(){

                    @Override
                    public void onNext(@NotNull PushResponseBean pushResponseBean) {
                        super.onNext(pushResponseBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }
}
